package com.zj.whichone.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.zj.whichone.bean.IndexVo;
import com.zj.whichone.bean.Menu;
import com.zj.whichone.bean.ReturnInfo;
import com.zj.whichone.bean.User;
import com.zj.whichone.bean.tree.TreeUtil;
import com.zj.whichone.constant.ReturnCode;
import com.zj.whichone.constant.UserRoot;

import cn.hn.java.summer.db.BaseDao;
import cn.hn.java.summer.db.multiple.DataSource1;
import cn.hn.java.summer.exception.BusinessException;
import cn.hn.java.summer.exception.MessageCodeException;
import cn.hn.java.summer.utils.DateUtils;
@Service
public class LoginService  extends BaseDao<DataSource1>{
	public User login(User user){
		logger.debug("LoginService -> login");
		return this.get("getUserByName",User.class, user);
	}
	
	public IndexVo index(User user) throws BusinessException{
    	if(user == null) throw new BusinessException("用户参数为空");
    	IndexVo iv = new IndexVo();
    	List<Menu> listMenu = this.list("getMenuByUser", Menu.class,user);
    	iv.setListTreeMenu(TreeUtil.getTree(listMenu));
    	iv.setUser(user);
    	return iv;
    }
	/*
	public List<Menu> index(User user) throws BusinessException{
		if(user == null) throw new BusinessException("用户参数为空");
		System.out.println("come in "+user);
		return this.list("getMenuByUser", Menu.class,user);
	}
	*/
	/**
	 * 用户注册
	 * @param user
	 * @throws BusinessException
	 * @throws MessageCodeException 
	 */
	public ReturnInfo regeist(User user) throws MessageCodeException{
		ReturnInfo rinfo = new ReturnInfo();
		if(this.get("getUserByName",User.class, user)!=null){
			rinfo.setCode(ReturnCode.REGEIST_HADREGEISTED);
			rinfo.setMsg("用户名为:"+user.getUsername()+"已注册!");
			return rinfo;
		}
		user.setRoot(UserRoot.getValue("普通玩家"));
		user.setMenuId(UserRoot.getValue("普通玩家"));
		user.setIsDel(0);
		user.setIsOnline(1);
		
		user.setCreateTime(DateUtils.now(DateUtils.YMDHMS2));
		if(this.update("insertUser", user)<1){
			rinfo.setCode(ReturnCode.REGEIST_FAIL);
			rinfo.setMsg("用户"+user.getUsername()+"注册失败!");
			return rinfo;
		}
		rinfo.setMsg("用户"+user.getUsername()+"注册成功!");
		return rinfo;
	}
}
